package com.hudongwx.shansun.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hudongwx.shansun.activity.LoginActivity;
import com.hudongwx.shansun.R;
import com.hudongwx.shansun.flashgrament.GoodkFragment;
import com.hudongwx.shansun.flashgrament.MyGoodFragment;

public class Fragment2 extends Fragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private Fragment[] fragments = new Fragment[2];
    private View view;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_fragment2, container, false);
            tabLayout = (TabLayout) view.findViewById(R.id.fragment_2_tab);
            viewPager = (ViewPager) view.findViewById(R.id.fragment_2_view);
            tabLayout.setupWithViewPager(viewPager);
            final TabLayout.Tab tab1 = tabLayout.newTab().setText("聚赞");
            TabLayout.Tab tab2 = tabLayout.newTab().setText("我的聚赞");
            tabLayout.addTab(tab1);
            tabLayout.addTab(tab2);
            MyFragment2Adapte myFragment2Adapte = new MyFragment2Adapte(getFragmentManager());
            viewPager.setAdapter(myFragment2Adapte);
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if (1 == position) {
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

        }
        return view;
    }

    class MyFragment2Adapte extends FragmentStatePagerAdapter {
        public MyFragment2Adapte(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (null == fragments[position]) {
                        fragments[position] = new GoodkFragment();
                    }
                    break;
                case 1:
                    if (null == fragments[position]) {
                        fragments[position] = new MyGoodFragment();
                    }
                    break;
            }

            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "聚赞";
            } else {
                return "我的聚赞";
            }

        }

    }

}
